package com.liuyjy.commons.masking.masking;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.deser.std.StdScalarDeserializer;

import java.io.IOException;
import java.util.Objects;

/**
 * @Author liuyjy
 * @Date 2022/7/18 20:43
 * @Description: 序列化器
 **/
public final class DataMaskingDeserializer extends StdScalarDeserializer<Object> {
    private final DataMaskingOperation operation;

    public DataMaskingDeserializer() {
        super(String.class);
        this.operation = null;
    }

    public DataMaskingDeserializer(DataMaskingOperation operation) {
        super(String.class);
        this.operation = operation;
    }

    @Override
    public Object deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException {
        //通过JsonParser获取字段的值
        Object maskChar = p.getValueAsString();
        if (!Objects.isNull(operation)) {
            maskChar = operation.mask(maskChar, null);
        }
        return maskChar;
    }



}
